#include "GameManager.h"

CGameManager::CGameManager()
{
	_dxInput=new CDxInput();
	_gr=new CGraphics();
	_sound=new SOUND();

	CGameSate::state=GameState::Menu;
	//CGameSate::state=GameState::Play;
}
CGameManager::~CGameManager()
{

}
bool CGameManager::Init(HINSTANCE hInst,HWND hwnd)
{
	if(!_gr->Init_Direct3D(hwnd,WINDOW_WIDTH,WINDOW_HEIGHT))
		return false;

	//initialize direct input
	if(!_dxInput->Init_DirectInput(hInst,hwnd))
	{
		MessageBox(hwnd, "Error initializing the direct input", "Error", MB_OK);
		return false;
	}
	//initialize keyboard
	if(!_dxInput->Init_Keyboard(hwnd))
	{
		MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
		return false;
	}
	if(!_dxInput->Init_Mouse(hwnd))
	{
		MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
		return false;
	}
	if(!_sound->Init(hwnd))
	{
		MessageBox(hwnd, "Error initializing the sound", "Error", MB_OK);
		return false;
	}
	CGameSate::_gr=_gr;
	CGameSate::_sound=_sound;
	CGameSate::_dxInput=_dxInput;
	//initialize game state
	LPD3DXSPRITE _spriteHandler;
	D3DXCreateSprite(_gr->_d3ddev,&_spriteHandler);
	CSprites::_spriteHandler=_spriteHandler;
	_gameMenu=CGameMenu::Singleton();
	_gamePlay=CGamePlay::Singleton(hwnd);
	_gameMenu->Game_Init();
	_gamePlay->Game_Init();
	return true;

}
void CGameManager::Run(HWND hwnd,DWORD tickPerFrame)
{
	switch (CGameSate::state)
	{
	case GameState::Menu:
		{
			_gamePointer=_gameMenu;
			break;
		}

	
	case GameState::LoadGame:
		{
			_gamePlay->Game_Load(true);
		}

	case GameState::Play:
		{
			CGameSate::state=GameState::Play;
			_gamePointer=_gamePlay;
			break;
		}
	default :
		{
			_gamePointer=_gameMenu;
			break;
		}
	}
	_gamePointer->Game_Run(hwnd,tickPerFrame);
}
void CGameManager::End()
{
	delete _gr;
	delete _sound;
	delete _dxInput;

	_gameMenu->Game_End();
	_gamePlay->Game_End();
}